package service

import (
	"context"
	"github.com/spf13/cast"
	"insighttracking/common/utils"
	"insighttracking/query/bo"
	"insighttracking/query/dao"
	"insighttracking/query/model"
)

type _inventoryRecordService struct{}

func (s _inventoryRecordService) CreateInventRecordApi(ctx context.Context, tx *dao.QueryTx, data *model.InventoryRecordEntity) error {
	if err := tx.InventoryRecordEntity.WithContext(ctx).Create(data); err != nil {
		return err
	}
	return nil
}

func (s _inventoryRecordService) BatchCreateInventRecordApi(ctx context.Context, tx *dao.QueryTx, data []*model.InventoryRecordEntity) error {
	if err := tx.InventoryRecordEntity.WithContext(ctx).CreateInBatches(data, len(data)); err != nil {
		return err
	}
	return nil
}

func (s _inventoryRecordService) GetInventoryRecordDetailPageApi(ctx context.Context, req bo.GetInventoryRecordDetailPageBoReq) (result []*model.InventoryRecordEntity, count int64, err error) {
	tx := dao.InventoryRecordEntity.WithContext(ctx).Where(dao.InventoryRecordEntity.EnterpriseID.Eq(req.EnterpriseID))
	if req.SpuID != "" {
		tx = tx.Where(dao.InventoryRecordEntity.SpuID.Eq(cast.ToInt64(req.SpuID)))
	}
	if req.Types != "" {
		tx = tx.Where(dao.InventoryRecordEntity.Types.Eq(cast.ToInt64(req.Types)))
	}
	if req.WarehouseID != "" {
		tx = tx.Where(dao.InventoryRecordEntity.WarehouseID.Eq(cast.ToInt64(req.WarehouseID)))
	}
	if req.StartDate != "" && req.EndDate != "" {
		tx = tx.Where(dao.InventoryRecordEntity.CreatedAt.Between(utils.DateTimeStrToTime(req.StartDate), utils.DateTimeStrToTime(req.EndDate)))
	}
	offset, limit := utils.DbPage(req.PageNumber, req.PageSize)
	return tx.Order(dao.InventoryRecordEntity.ID.Desc()).FindByPage(offset, limit)
}

var InventRecordService = new(_inventoryRecordService)
